package com.naiterui.ehp.bs.cms.modules.recommend.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.bo.esign.PreSignResultBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.security.utils.SecurityUtils;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.utils.esign.ESignUtils;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.recommend.service.IMedicationRecomService;
import com.naiterui.ehp.bs.cms.modules.recommend.vo.ESignConfigVO;
import com.naiterui.ehp.bs.cms.modules.recommend.vo.MedicationRecomDetailVO;
import com.naiterui.ehp.bs.cms.modules.recommend.vo.MedicationRecomVO;
import com.naiterui.ehp.bs.cms.modules.recommend.vo.QrcodeVO;
import com.naiterui.ehp.bs.cms.modules.recommend.vo.RecomAuditOperationLogVO;
import com.naiterui.ehp.bs.cms.modules.recommend.vo.VerifyIdentityVO;
import com.naiterui.ehp.bs.cms.modules.user.feign.PharmacisFeignClient;
import com.naiterui.ehp.bs.cms.modules.user.vo.SelectPharmacistVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 处方列表
 *
 * @author guoyongxiang
 * @date 2019-06-18 15:15
 * @since 1.0.0
 */
@Slf4j
@Validated
@Api(tags = {SwaggerTag.CMS + SwaggerTag.RECOM})
@RestController
@RequestMapping("medication/recom")
public class MedicationRecomController {

    @Autowired
    private IMedicationRecomService medicationRecomService;

    @Autowired
    private PharmacisFeignClient pharmacisFeignClient;

    /**
     * 列表
     *
     * @return
     */
    @ApiOperation("处方列表")
    @SysLog("处方列表")
    @GetMapping("/list")
    @PreAuthorize("@p.check('medication:recom:list')")
    public PageVO<MedicationRecomVO> list(PageParamsVO pageParamsVO, MedicationRecomVO recom) throws BusinessException {
        return medicationRecomService.recomList(pageParamsVO, recom, false);
    }

    /**
     * 审核处方列表
     *
     * @return
     */
    @ApiOperation("审核处方列表")
    @SysLog("审核处方列表")
    @GetMapping("/audit/list")
    @PreAuthorize("@p.check('medication:recom:list')")
    public PageVO<MedicationRecomVO> auditList(PageParamsVO pageParamsVO, MedicationRecomVO recom)
        throws BusinessException {
        return medicationRecomService.recomList(pageParamsVO, recom, true);
    }

    @ApiOperation("处方详情")
    @ApiImplicitParam(name = "recomId", value = "处方ID", dataType = "long", paramType = "query", required = true)
    @SysLog("处方详情")
    @GetMapping("/detail")
    @PreAuthorize("@p.check('medication:recom:detail')")
    public MedicationRecomDetailVO detail(Long recomId) throws BusinessException {
        return medicationRecomService.recomDetail(recomId, false);
    }

    @ApiOperation("审核处方详情")
    @ApiImplicitParam(name = "recomId", value = "处方ID", dataType = "long", paramType = "query", required = true)
    @SysLog("审核处方详情")
    @GetMapping("/audit/detail")
    @PreAuthorize("@p.check('medication:recom:detail')")
    public MedicationRecomDetailVO auditDetail(Long recomId) throws BusinessException {
        return medicationRecomService.recomDetail(recomId, true);
    }

    @ApiOperation("处方操作日志")
    @ApiImplicitParam(name = "recomId", value = "处方ID", dataType = "long", paramType = "query", required = true)
    @SysLog("处方操作日志")
    @GetMapping("/logs/audit")
    @PreAuthorize("@p.check('medication:recom:detail')")
    public PageVO<RecomAuditOperationLogVO> auditLogs(PageParamsVO pageParamsVO, Long recomId) {
        return medicationRecomService.auditLogs(pageParamsVO, recomId);
    }

    @ApiOperation("处方预签名(发送验证码)")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "recomId", value = "处方ID", dataType = "long", paramType = "form", required = true),})
    @SysLog("处方预签名(发送验证码)")
    @PostMapping("preSign")
    @PreAuthorize("@p.check('medication:recom:audit')")
    public PreSignResultBO preSign(Long recomId) throws BusinessException {
        Long userId = SecurityUtils.getCurrentUser().getId();
        return medicationRecomService.preSign(userId, recomId);
    }

    @ApiOperation("处方审核")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "recomId", value = "处方ID", dataType = "long", paramType = "form", required = true),
        @ApiImplicitParam(name = "status", value = "审核状态(1:通过,2:不通过)", dataType = "int", paramType = "form",
            required = true),
        @ApiImplicitParam(name = "signPwd", value = "签名密码", dataType = "String", paramType = "form", required = true),
        @ApiImplicitParam(name = "uniqueId", value = "唯一标识", dataType = "String", paramType = "form", required = true),
        @ApiImplicitParam(name = "remark", value = "操作备注(不通过时原因必填)", dataType = "String", paramType = "form",
            required = false)})
    @SysLog("处方审核")
    @PostMapping("audit")
    @PreAuthorize("@p.check('medication:recom:audit')")
    public void audit(Long recomId, Integer status, String remark, String uniqueId, Integer force, String signPwd)
        throws BusinessException {
        Long userId = SecurityUtils.getCurrentUser().getId();
        log.info("处方审核userId:{},recomId：{},status:{},remark:{},force:{},signPwd:{},uniqueId:{}", userId, recomId,
            status, remark, force, signPwd, uniqueId);
        medicationRecomService.audit(userId, recomId, status, remark, force, signPwd, uniqueId);
    }

    @ApiOperation("校验药师身份")
    @SysLog("校验药师身份")
    @GetMapping("verify/identity")
    @PreAuthorize("@p.check('medication:recom:verify:identity')")
    public VerifyIdentityVO verifyIdentity() {
        Long userId = SecurityUtils.getCurrentUser().getId();
        log.info("校验药师身份userId：{}", userId);
        return medicationRecomService.verifyIdentity(userId);
    }

    @ApiOperation("设置药师身份")
    @SysLog("设置药师身份")
    @PostMapping("identity/{pharmacistId}/{idCard}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pharmacistId", value = "药师ID", dataType = "long", paramType = "path",
            required = true),
        @ApiImplicitParam(name = "idCard", value = "身份证号", dataType = "string", paramType = "path", required = true),})
    @PreAuthorize("@p.check('medication:recom:set:identity')")
    public void setIdentity(@PathVariable Long pharmacistId, @PathVariable String idCard) throws BusinessException {
        Long userId = SecurityUtils.getCurrentUser().getId();
        log.info("设置药师身份 pharmacistId:{},idCard:{}, userId：{}", pharmacistId, idCard, userId);
        medicationRecomService.setIdentity(userId, pharmacistId, idCard);
    }

    @ApiOperation("设置药师身份")
    @SysLog("设置药师身份")
    @PostMapping("identityuncheck/{phone}/{idCard}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "药师手机号", dataType = "string", paramType = "path", required = true),
        @ApiImplicitParam(name = "idCard", value = "身份证号", dataType = "string", paramType = "path", required = true),})
    public void setIdentityUnCheck(@PathVariable String phone, @PathVariable String idCard) throws BusinessException {
        Long userId = SecurityUtils.getCurrentUser().getId();
        log.info("设置药师身份 phone:{},idCard:{}, userId：{}", phone, idCard, userId);
        medicationRecomService.setIdentityUncheck(userId, phone, idCard);
    }

    @ApiOperation("发送药师实名校验验证码")
    @SysLog("发送药师实名校验验证码")
    @PostMapping("sendRealnameCode/{phone}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "药师手机", dataType = "long", paramType = "path", required = true),})
    public void sendRealnameCode(@PathVariable String phone) throws BusinessException {
        log.info("发送药师实名校验验证码 phone:{}", phone);

        medicationRecomService.realnameCode(phone);
    }

    @ApiOperation("根据手机号和验证码获取药师")
    @SysLog("根据手机号和验证码获取药师")
    @PostMapping("pharmacist/{phone}/{verifyCode}")
    @PreAuthorize("@p.check('medication:recom:pharmacist')")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "phone", value = "手机号", dataType = "string", paramType = "path", required = true),
        @ApiImplicitParam(name = "verifyCode", value = "验证码", dataType = "string", paramType = "path",
            required = true),})
    public SelectPharmacistVO getPharmacistByPhone(@PathVariable String phone, @PathVariable String verifyCode)
        throws BusinessException {
        log.info("根据手机号和验证码获取药师 phone:{},verifyCode:{}", phone, verifyCode);

        return medicationRecomService.getPharmacistByPhone(phone, verifyCode);
    }

    @ApiOperation("获取绑定药师二维码")
    @ApiImplicitParam(name = "uuid", value = "uuid", dataType = "string", paramType = "query", required = true)
    // @SysLog("获取绑定药师二维码")
    @GetMapping("qrcode")
    public QrcodeVO qrcode(String uuid) throws BusinessException {
        Long userId = SecurityUtils.getCurrentUser().getId();
        log.info("获取绑定药师二维码 userId:{}, uuid={}", userId, uuid);
        return medicationRecomService.qrcode(userId, uuid);
    }

    @ApiOperation("解绑药师")
    @SysLog("解绑药师")
    @PostMapping("untie")
    @PreAuthorize("@p.check('medication:recom:untie')")
    public void untie() throws BusinessException {
        Long userId = SecurityUtils.getCurrentUser().getId();
        log.info("解绑药师 userId:{}", userId);
        medicationRecomService.untie(userId);
    }

    @ApiOperation("处方单药师签名预签名")
    @ApiImplicitParams({@ApiImplicitParam(value = "处方id", name = "recomId", required = true, dataType = "long"),
        @ApiImplicitParam(value = "药师id", name = "pharmacistId", required = true, dataType = "long")})
    @PostMapping("/sign/pre")
    @PreAuthorize("@p.check('medication:recom:sign:pre')")
    public PreSignResultBO preSign(Long recomId, Long pharmacistId) throws BusinessException {
        return pharmacisFeignClient.preSign(recomId, pharmacistId);
    }

    @ApiOperation("获取签名厂商")
    @GetMapping("/esign/service")
    public ESignConfigVO getEsignServiceString() {
        // 签名配置
        ESignConfigVO eSignConfigVO = new ESignConfigVO();
        eSignConfigVO.setSeviceName(ESignUtils.getServiceId());
        eSignConfigVO.setSealType(ESignUtils.getSealType());
        return eSignConfigVO;
    }
}
